package org.aaron.demos;

import java.util.List;

import org.aaron.demos.mapsoverlay.HelloItemizedOverlay;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MapViewActivity extends MapActivity {
	private MapView mapView;
	private MapController controller;
	private Projection projection;
	private List<Overlay> overlays;
	private GeoPoint beginPoint, endPoint;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.mapviewlayout);
		init();
	}

	private void init() {
		mapView = (MapView) findViewById(R.id.mymapview);
		mapView.setBuiltInZoomControls(true);
		overlays = mapView.getOverlays();
		projection = mapView.getProjection();
		controller = mapView.getController();
		beginPoint = new GeoPoint(19240000, -99120000);
		endPoint = new GeoPoint(35410000, 139460000);

		// Drawable drawable =
		// this.getResources().getDrawable(R.drawable.ic_launcher);
		// HelloItemizedOverlay helloItemizedOverlay = new
		// HelloItemizedOverlay(drawable, this);
		// OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
		// "I'm in Mexico City!");
		// helloItemizedOverlay.addOverlay(overlayitem);
		// OverlayItem overlayitem2 = new OverlayItem(point2,
		// "Sekai, konichiwa!", "I'm in Japan!");
		// helloItemizedOverlay.addOverlay(overlayitem2);
		overlays.add(new PointOverlay(beginPoint));
		overlays.add(new PointOverlay(endPoint));
		overlays.add(new LineOverLay(beginPoint, endPoint));
		controller.animateTo(beginPoint);
		controller.setZoom(12);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	class PointOverlay extends Overlay {
		GeoPoint geoPoint;

		public PointOverlay() {

		}

		public PointOverlay(GeoPoint point) {
			this.geoPoint = point;
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			Point point = new Point();
			projection.toPixels(geoPoint, point);
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tag_32);
			Paint paint = new Paint();
			canvas.drawBitmap(bitmap, point.x, point.y, paint);
		}
	}

	class LineOverLay extends Overlay {
		private GeoPoint geoendPoint, geobeginPoint;

		public LineOverLay() {
		}

		public LineOverLay(GeoPoint beginPoint, GeoPoint endPoint) {
			this.geobeginPoint = beginPoint;
			this.geoendPoint = endPoint;
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			Paint paint = new Paint();
			paint.setColor(Color.BLUE);
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			paint.setStrokeWidth(2);
			Point beginPoint = new Point();
			Point endPoint = new Point();
			Path path = new Path();
			projection.toPixels(geobeginPoint, beginPoint);
			projection.toPixels(geoendPoint, endPoint);
			path.moveTo(endPoint.x+32, endPoint.y+32);
			path.lineTo(beginPoint.x+32, beginPoint.y+32);
			canvas.drawPath(path, paint);
		}
	}
}
